package com.cqs.learning.spring.cloud.app.controller;

import com.cqs.learning.spring.cloud.app.client.IConsumer;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * Created by li on 20-3-1.
 */
@RequestMapping
@RestController
@RefreshScope
@Log4j2
public class ConsumerController {


    //    @Value("${env:test}")
    @Value("${env}")
    private String env;

    @Autowired
    public IConsumer consumer;

    @GetMapping("/cnt")
    public String getCnt() {
        String count = consumer.cnt();
        System.out.println("remote service return cnt: " + count);
        return "remote service return cnt: " + count;
    }

    @RefreshScope
    @GetMapping("/env")
    public String env() {
        log.info("环境变量env:{} ", env);
        return "环境变量env:" + env;
    }


    public static void main(String[] args) throws InterruptedException {
        ExecutorService executor = Executors.newScheduledThreadPool(1, new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                t.setName("com.alibaba.nacos.client.Worker.");
                t.setDaemon(true);
                return t;
            }
        });
        AtomicInteger cnt = new AtomicInteger();
        ((ScheduledExecutorService) executor).scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                log.info("==" + cnt.addAndGet(1));
            }
        }, 1L, 10, TimeUnit.MILLISECONDS);

        TimeUnit.SECONDS.sleep(1000);
    }

}
